<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/** Profile Page **/
class Profile extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model("users");
		$this->load->model("posts");
		$this->load->helper('text');
	}
	
	public function index($id)
	{
		$value['title']  = "Profile";
		$value['news']	 = $this->posts->getAllRecords(3,10,'now');
		$value['comment']= $this->posts->getCommentByPost($id);
		$value['profile']= $this->users->getUserData($id);
		$value['myprofile']= $this->users->getUserData($this->session->userdata('user_login'));
		$value['my']	 = $id; 	
		$value['content']= "vuser/vprofile";
		$this->load->view('themes/bdo/index',$value);
	}
	
	function My($id){
		if($id<>$this->session->userdata('user_login'))
			redirect('profile/My/'.$this->session->userdata('user_login'));
		$value['title']  = "Profile";
		$value['profile']= $this->users->getUserData($id);
		$value['content']= "vuser/vmy";
		$this->load->view('themes/bdo/index',$value);	
	}
	
	public function uploadImage(){
		chmod('./assets/avatar/',0775);
		$config['upload_path'] = './assets/avatar/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	   = '0';
		$config['max_width']   = '0';
		$config['max_height']  = '0';
		$config['file_name']   = date('Y-m-d H:i:s').'-'.$this->session->userdata('login');
		$this->load->library('upload', $config);
		if (!$this->upload->do_upload('image')):
			redirect('err');
		else:
			$data = $this->upload->data();
		endif;
		
		if(!empty($data))
			$image = $data['file_name'];
		else
			$image = '';
		return $image;
	}
	
	public function updateProfile(){
		$avatar = $this->uploadImage(); // call AVA
		$this->users->updateProfile($avatar);
		redirect($this->input->server('HTTP_REFERER'),301);
	}
	
	function addPost(){
		echo $this->posts->inserttodb();
	}

	function addComment(){
		echo $this->posts->inserttoComment();
	}	
	
	function Employee(){
		$value['title']  = "Profile";
		$value['news']	 = $this->posts->getAllRecords(2,10,'now');
		$value['users']  = $this->users->getEmployee(1);
 		$value['profile']= $this->users->getUserData($this->session->userdata('user_login'));
		$value['content']= "vuser/vemployee";
		$this->load->view('themes/bdo/index',$value);
	}
	
	function userlog(){
		$id = $this->session->userdata('user_login');
		if(!is_superUser())
			redirect('profile/My/'.$id);
		if($this->session->userdata('date_log'))
			$date = $this->session->userdata('date_log');
		else
			$date = date('d-m-Y');
		$value['title']  = "Profile";
		$value['date']   = $date;
		$value['profile']= $this->users->getUserData($id);
		$value['logs']   = $this->users->getUserLog($date);
		$value['content']= "vuser/vlog";
		$this->load->view('themes/bdo/index',$value);	
	}
	
	function do_log(){
		$date = $this->input->post('day').'-'.$this->input->post('month').'-'.$this->input->post('year');
		$this->session->set_userdata('date_log',$date);
		redirect($this->input->server('HTTP_REFERER'),301);
	}
}

/* End of file profile.php */
/* Location: ./application/controllers/profile.php */